package com.cheyun.netsalev3.repository.aftersale

import com.cheyun.netsalev3.bean.aftersale.DriveDetail
import com.cheyun.netsalev3.bean.aftersale.DrivePlanData
import com.cheyun.netsalev3.bean.aftersale.EditDriveBack
import com.cheyun.netsalev3.bean.basedata.Member
import com.cheyun.netsalev3.utils.CommonFunc
import com.cheyun.netsalev3.utils.api.ApiCallback
import com.cheyun.netsalev3.utils.api.ApiClient
import com.cheyun.netsalev3.utils.api.ApiException
import com.cheyun.netsalev3.utils.api.BaseResp
import com.google.gson.Gson
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.*

class EditDriveRepository {
    fun updateData(data: DriveDetail, onSuccess: (back:EditDriveBack) -> Unit) {
        var json = Gson().toJson(data)
        var map = CommonFunc.gson.fromJson<TreeMap<String, String>>(json, CommonFunc.typeToken)
        ApiClient.getInstance().editTakecar(map)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<EditDriveBack>>() {
                    override fun onSuccess(t: BaseResp<EditDriveBack>) {
                        t.data?.let { onSuccess(it) }
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    fun drivePublishOrder(plantotalfee:String,planmileage:String,id:Int,onSuccess: () -> Unit){
        ApiClient.getInstance().drivePublishOrder(plantotalfee,planmileage,id)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        onSuccess()
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }


    fun getPlanPrice(id:Int, onSuccess: (back:DrivePlanData) -> Unit) {
        ApiClient.getInstance().getPlanPrice(id)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<DrivePlanData>>() {
                    override fun onSuccess(t: BaseResp<DrivePlanData>) {
                        t.data?.let { onSuccess(it) }
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    fun cancelOrder(id:Int,onSuccess: () -> Unit){
        ApiClient.getInstance().cancelDriveOrder(id)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        onSuccess()
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    fun cancelDrive(id:Int,onSuccess: () -> Unit){
        ApiClient.getInstance().cancelSingleDriveOrder(id)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        onSuccess()
                    }

                    override fun onError(ex: ApiException) {

                    }
                })
    }

    fun allocation(mid: String,serciveuid:String,
                   moth: () -> Unit, fail: () -> Unit) {
        ApiClient.getInstance().allocationDrive(mid,serciveuid)
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<Any>>() {
                    override fun onSuccess(t: BaseResp<Any>) {
                        moth()
                    }

                    override fun onError(ex: ApiException) {
                        fail()
                    }

                })
    }

    fun getAftersaleUsableUserList(onSuccess: (data :List<Member>) -> Unit) {
        ApiClient.getInstance().getAftersaleUsableUserList()
                .subscribeOn(Schedulers.io())
                .subscribeOn(AndroidSchedulers.mainThread())
                .subscribe(object : ApiCallback<BaseResp<List<Member>>>() {
                    override fun onSuccess(t: BaseResp<List<Member>>) {
                        onSuccess(t.data!!)
                    }

                    override fun onError(ex: ApiException) {

                    }


                })
    }



}